using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace Niu
{
    public partial class nListView : System.Windows.Forms.ListView
    {
        private Hashtable hash = new Hashtable();

        private Boolean m_ProEnabled = false;
        private Int32 m_ProIndex = 0;
        private Color m_ProBgColor = Color.Gray;
        private Color m_ProColor = Color.Blue;
        private Color m_ProTxtColor = Color.Black;

        private void DrawProgress(Rectangle Rect, Int32 Percent, Graphics G)
        {
            SizeF sizef = G.MeasureString("###%", this.Font);
            Int32 rigth_width = (Int32)(sizef.Width + (Single)0.5);

            Int32 left_width = Rect.Width - rigth_width;
            Int32 bar_width = (Int32)(left_width * ((Single)Percent / 100) + (Single)0.5);

            String str = Percent.ToString() + "%";
            sizef = G.MeasureString(str, this.Font);
            Int32 str_width = (Int32)(sizef.Width + 1);

            if (Rect.Width < str_width)
            {
                return;
            }

            using (Brush brush = new SolidBrush(m_ProBgColor))
            {
                Rectangle rect = new Rectangle(Rect.Left, Rect.Top, left_width, Rect.Height);

                G.FillRectangle(brush, rect);
            }

            using (Brush brush = new SolidBrush(m_ProColor))
            {
                Rectangle rect = new Rectangle(Rect.Left, Rect.Top, bar_width, Rect.Height);

                G.FillRectangle(brush, rect);
            }


            using (Brush brush = new SolidBrush(m_ProTxtColor))
            {
                Rectangle rect = new Rectangle(Rect.Right - str_width, Rect.Top, str_width, Rect.Height);

                G.DrawString(str, this.Font, brush, rect);
            }  
        }

        protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;

            base.OnDrawColumnHeader(e);
        }

        protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
        {
            if (hash.ContainsKey(e.Item) == true && e.ColumnIndex == m_ProIndex && m_ProEnabled == true)
            {
                Int32 Value = (Int32) hash[e.Item];
                Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
                DrawProgress(rect, Value, e.Graphics);
            }
            else
            {
                e.DrawDefault = true;

                base.OnDrawSubItem(e);
            }
        }

        public nListView()
        {
            this.OwnerDraw = false;
        }

        public Boolean ProEnabled
        {
            get
            {
                return m_ProEnabled;
            }
            set
            {
                this.OwnerDraw = value;
                m_ProEnabled = value;
            }
        }

        public Int32 ProIndex
        {
            get
            {
                return m_ProIndex;
            }
            set
            {
                m_ProIndex = value;
            }
        }

        public Color ProBgColor
        {
            get
            {
                return m_ProBgColor;
            }
            set
            {
                m_ProBgColor = value;
            }
        }

        public Color ProColor
        {
            get
            {
                return m_ProColor;
            }
            set
            {
                m_ProColor = value;
            }
        }

        public Color ProTxtColor
        {
            get
            {
                return m_ProTxtColor;
            }
            set
            {
                m_ProTxtColor = value;
            }
        }

        public void ProValue(Int32 Index, Int32 Value)
        {
            if (Index > this.Items.Count)
            {
                return;
            }

            ProValue(this.Items[Index], Value);
        }

        public void ProValue(ListViewItem Item, Int32 Value)
        {
            if (this.Items.Contains(Item) == false)
            {
                return;
            }

            if (Value < 0)
            {
                Value = 0;
            }

            if (Value > 100)
            {
                Value = 100;
            }

            if (hash.ContainsKey(Item) == true)
            {
                hash[Item] = Value;
            }
            else
            {
                hash.Add(Item, Value);
            }

            if (m_ProEnabled == true)
            {
                this.RedrawItems(Item.Index, Item.Index, false);
            }
        }

        public Int32 ProValue(Int32 Index)
        {
            if (Index > this.Items.Count)
            {
                return 0;
            }

            return ProValue(this.Items[Index]);
        }

        public Int32 ProValue(ListViewItem Item)
        {
            if (hash.ContainsKey(Item) == true)
            {
                return (Int32)hash[Item];
            }
            else
            {
                return 0;
            }
        }

    }

}
